import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/index',
    }, {
        path: '/center',
        name: 'center',
        component: () =>
            import ('../pages/center.vue')
    },
    {
        path: '/play',
        name: 'play',
        component: () =>
            import ('../pages/play.vue')
    },
    {

        path: '/index',
        name: 'index',
        component: () =>
            import ('../pages/index.vue')
    },
    {
        path: '/circle',
        name: 'circle',
        component: () =>
            import ('../pages/circle.vue')
    },
    {
        path: '/search',
        name: 'search',
        component: () =>
            import ('../pages/search.vue'),
    },
    {
        path: '/histroy',
        name: 'histroy',
        component: () =>
            import ('../pages/histroy.vue')
    },
    {
        path: '/songdetails',
        name: 'songdetails',
        component: () =>
            import ('../pages/songdetails.vue')
    },
    {
        path: '/songlist',
        name: 'songlist',
        component: () =>
            import ('../pages/songlist.vue')
    },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ('../pages/login.vue')
    },
    {
        path: '/register',
        name: 'register',
        component: () =>
            import ('../pages/register.vue')
    },
    {
        path: '/album',
        name: 'album',
        component: () =>
            import ('../pages/album.vue')
    },
    {
        path: '/comment',
        name: 'comment',
        component: () =>
            import ('../pages/comment.vue')
    },
    {
        path: '/singer',
        name: 'singer',
        component: () =>
            import ('../pages/singer.vue')
    },
    {
        path: '/singerall',
        name: 'singerall',
        component: () =>
            import ('../pages/singerall.vue')
    },
    {
        path: '/newsong',
        name: 'newsong',
        component: () =>
            import ('../pages/newsong.vue')
    },
]

const router = new VueRouter({
    // mode: 'history',
    base: process.env.BASE_URL,
    routes,
    linkActiveClass: "active"
})

export default router

const nodownarr = [{
        name: 'search',
        path: '/search'
    },
    {
        name: 'histroy',
        path: '/histroy'
    },
    {
        name: 'songlist',
        path: '/songlist'
    },
    {
        name: 'songdetails',
        path: '/songdetails'
    },
    {
        name: 'login',
        path: '/login'
    },
    {
        name: 'register',
        path: '/register'
    },
    {
        name: 'album',
        path: '/album',
    },
    {
        name: 'comment',
        path: '/comment',
    },
    {
        name: 'singer',
        path: '/singer',
    },
    {
        name: 'singerall',
        path: '/singerall',
    },
    {
        name: 'newsong',
        path: '/newsong',
    },
]

router.beforeEach((to, from, next) => {
    if (to.matched.length === 0) {
        //如果未匹配到路由
        console.log("未找到路由", to)
        from.path ? next({ path: from.path }) : next('/'); //如果上级也未匹配到路由则跳转主页面，如果上级能匹配到则转上级路由
    } else {
        for (let i = 0; i < nodownarr.length; i++) {
            if (to.name == nodownarr[i].name || to.path == nodownarr[i].path) {
                // console.log("不显示")
                to.query.isshow = false
                next();
                return
            }
        }
        // console.log("显示")
        to.query.isshow = true
        next();
    }
});